package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Expire;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("ExpireController")
public class ExpireController extends CmiEmaController {

	@RequestMapping("/listExpires")
	public ModelAndView listExpires(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = expirePaginationService.updatePagination(request, pagination);
		Set<Expire> expires = expirePaginationService.getPaginationExpires(pagination);

		mav.addObject("expires", expires);
		mav.addObject("entityName", "expire");
		mav.addObject("pagination", pagination);
		mav.setViewName("expire/listExpires");
		return mav;
	}

	public String listExpires() {
		return "redirect:/listExpires";
	}

	@RequestMapping("/viewExpire")
	public ModelAndView viewExpire(@RequestParam Integer expireId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("expire", expireDAO.findExpireByPrimaryKey(expireId));
		mav.setViewName("expire/viewExpire");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteExpires")
	public ModelAndView confirmDeleteExpires(
			@RequestParam(value = "deleteExpiresIds", required = true) Set<Integer> deleteExpiresIds) {
		ModelAndView mav = new ModelAndView();

		Set<Expire> deleteExpires = expireDAO.findExpiresByIds(deleteExpiresIds);
		Set<Expire> referencedExpires = expireService.loadReferenceExpires(deleteExpires);
		boolean hasReferenced;
		if (referencedExpires.size() == 0) {
			mav.addObject("deleteEntitys", deleteExpires);
			hasReferenced = false;
		} else {
			deleteExpires.removeAll(referencedExpires);
			mav.addObject("referencedEntitys", referencedExpires);
			mav.addObject("deleteEntitys", deleteExpires);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Expire");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listExpires");
		return mav;
	}

	@RequestMapping("/deleteExpire")
	public String deleteExpire(@RequestParam Integer expireId) {
		Expire expire = expireDAO.findExpireByPrimaryKey(expireId);
		expireService.deleteExpire(expire);
		return "forward:/listExpires";
	}

	@RequestMapping("/confirmDeleteExpire")
	public ModelAndView confirmDeleteExpire(@RequestParam Integer expireId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("expire", expireDAO.findExpireByPrimaryKey(expireId));

		mav.setViewName("expire/deleteExpire");

		return mav;
	}

	@RequestMapping("/deleteExpires")
	public String deleteExpires(
			@RequestParam(value = "deleteExpiresId", required = true) Set<Integer> deleteExpiresId) {
		expireService.deleteExpires(deleteExpiresId);
		return "forward:/listExpires";
	}

	@RequestMapping("/editExpire")
	public ModelAndView editExpire(@RequestParam Integer expireId) {

		ModelAndView mav = new ModelAndView();

		Expire expire = expireDAO.findExpireByPrimaryKey(expireId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("expire", expire);

		// 把所有的引用expire对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(expireService.loadExpireObjectReferencedSet(expire));

		// 将编辑页面expire添加到mav中
		mav.setViewName("expire/editExpire");
		return mav;
	}

	@RequestMapping("/newExpire")
	public ModelAndView newExpire() {
		ModelAndView mav = new ModelAndView();
		Expire expire = new Expire();

		mav.addObject("expire", expire);

		// 把所有的引用expire对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(expireService.loadExpireObjectReferencedSet(expire));

		mav.addObject("newFlag", true);
		mav.setViewName("expire/editExpire");

		return mav;
	}

	@RequestMapping("/saveExpire")
	public ModelAndView saveExpire(@ModelAttribute Expire expire) {

		ModelAndView mav = new ModelAndView();

		Unit unitTemp = expire.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Expire existingExpire = CmiHibernateProxyUtil
				.deproxy(expireDAO.findExpireByPhysicalPrimaryKey(expire.getQuantity(), unit.getId()));

		expire.setUnit(unit);
		// 新建记录
		if (expire.getId() == null) {
			if (expire.equals(existingExpire)) {

				mav.addAllObjects(expireService.loadExpireClassReferencedSet(expire));
				// mav = editContinue(mav, expire);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("expire/editExpire");
			} else {
				expireService.saveExpire(expire);
				mav.setViewName("forward:/listExpires");
			}
		}
		// 编辑记录
		else {
			Expire originExpire = CmiHibernateProxyUtil.deproxy(expireDAO.findExpireByPrimaryKey(expire.getId()));
			if (!expire.equals(originExpire) && expire.equals(existingExpire)) {
				mav.addAllObjects(expireService.loadExpireClassReferencedSet(expire));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("expire/editExpire");
			} else {
				expireService.saveExpire(expire);
				mav.setViewName("forward:/listExpires");
			}
		}
		return mav;
	}

	@RequestMapping("/expireController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
